package LeetCode.leetcode;

/**
 * 最后一个单词的长度
 *
 * @author: leiwenkang02@meituan.com
 * @date: 2018/8/24
 * @time: 09:51
 */
public class Test58最后一个单词的长度 {
    public static void main(String[] args) {
        System.out.println(lengthOfLastWord(" "));
    }

    public static int lengthOfLastWord(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        char[] ch = s.toCharArray();
        int start = 0;
        int end = 0;
        for (int i = ch.length - 1; i >= 0; i--) {
            if (ch[i] == ' ') {
                if (start == end) {
                    start++;
                    end++;
                } else {
                    break;
                }

            } else {
                end++;
            }
        }
        return end - start;
    }
}
